const db = require("../db/index");
// 发布评论
exports.IssueMessage = (req, res) => {
  const messageInfo = {
    ...req.body,
    avatar: req.user.user_pic,
    nickname: req.user.username,
    create_time: new Date(),
  };
  console.log(messageInfo.avatar);
  const sql = `insert into ev_message set ?`;
  db.query(sql, messageInfo, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("评论失败");
    res.send({ status: 0, message: "评论成功!" });
  });
};
// 获取评论
exports.getMessage = (req, res) => {
  const sql = `select * from ev_message where is_delete=0 order by  id desc`;
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 0,
      message: "评论获取成功!",
      data: results,
      total: results.length,
    });
  });
};
// 删除评论
exports.delMessage = (req, res) => {
  const sql = `update ev_message set is_delete = 1 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("删除评论失败!");
    res.send({ status: 0, message: "删除评论成功!" });
  });
};
